package com.ning.activitys.skysound.fragment.mymusic;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;
import com.ning.activitys.skysound.R;
import com.ning.activitys.skysound.activity.MainActivity;
import com.ning.activitys.skysound.adpter.AllMusicAdapter;
import com.ning.activitys.skysound.base.BaseFragment;
import com.ning.activitys.skysound.base.MyApplication;
import com.ning.activitys.skysound.beans.MusicDetailsBeans;
import com.ning.activitys.skysound.service.PlayService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/16.
 */
public class LikeMusicFragment extends BaseFragment implements AdapterView.OnItemClickListener {

    private ListView listView;
    private AllMusicAdapter adapter;
    private MainActivity mainActivity;
    private MyApplication myApplication;

    private volatile static LikeMusicFragment likeMusicFragment;//单例的引用

    //单例模式创建对象
    public static LikeMusicFragment getInstance() {
        if (likeMusicFragment == null) {
            //线程锁,只有一个线程可以调用
            synchronized (LikeMusicFragment.class) {
                if (likeMusicFragment == null) {
                    likeMusicFragment = new LikeMusicFragment();
                }
            }
        }
        return likeMusicFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_like_music,null);

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mainActivity = (MainActivity) getActivity();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        myApplication = (MyApplication) mainActivity.getApplication();

        listView = (ListView) view.findViewById(R.id.list_view_like_music);


        MyApplication myApplication = (MyApplication) mainActivity.getApplication();
        ArrayList<MusicDetailsBeans> musicDetailsData = new ArrayList<>();
        try {
            musicDetailsData = (ArrayList)myApplication.
                    dbUtils.findAll(Selector.from(MusicDetailsBeans.class).where("isLike","=","1"));

            //?????????
            if (musicDetailsData == null || musicDetailsData.size() == 0){
                return;
            }

        } catch (DbException e) {
            e.printStackTrace();
        }


        adapter = new AllMusicAdapter(mainActivity,musicDetailsData);
        listView.setAdapter(adapter);


        listView.setOnItemClickListener(this);


    }

    @Override
    public void onResume() {
        super.onResume();
        mainActivity.bindPlayService();
//
//        if (null == adapter) {
//            Toast.makeText(mainActivity, "kong", Toast.LENGTH_SHORT).show();
//        }else {
//
//            adapter.notifyDataSetChanged();
//            Toast.makeText(mainActivity, "BU_KONG", Toast.LENGTH_SHORT).show();
//        }
    }

    @Override
    public void onPause() {
        super.onPause();
        mainActivity.unBindPlayService();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mainActivity.unBindPlayService();
    }

    /**
     * ListView item 单击事件
     * */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        PlayService.IS_NET_MUSIC = 1;

        if (mainActivity.playService.getPlatList() != PlayService.LIKE_MUSIC) {

            mainActivity.playService.setPlatList(PlayService.LIKE_MUSIC);

            try {

                mainActivity.playService.setMusicDetailsData
                        ((ArrayList)myApplication.dbUtils.findAll(Selector.from(MusicDetailsBeans.class).where("isLike","=","1")));

            } catch (DbException e) {
                e.printStackTrace();
            }
        }
         mainActivity.playService.play(position);



        /**
         * 点击添加最近播放
         * */
        MusicDetailsBeans musicDetailsBeans = mainActivity.playService.getMusicDetailsData().get(position);

        MyApplication myApplication = (MyApplication) mainActivity.getApplication();
        try {

            MusicDetailsBeans beans =  myApplication.dbUtils.findFirst(Selector.from(MusicDetailsBeans.class)
                    .where("myID", "=", musicDetailsBeans.getMyID()));


            if (beans == null){
                musicDetailsBeans.setMyID(musicDetailsBeans.getId());
                musicDetailsBeans.setPlayRecentTime(System.currentTimeMillis());
                myApplication.dbUtils.save(musicDetailsBeans);
            }else {
                myApplication.dbUtils.update(musicDetailsBeans,"playRecentTime");
            }

        } catch (DbException e) {
            e.printStackTrace();
        }
    }







    public void initData(){
        ArrayList<MusicDetailsBeans> musicDetailsData;
        musicDetailsData = (ArrayList<MusicDetailsBeans>) mainActivity.playService.getMusicDetailsData();
        adapter = new AllMusicAdapter(mainActivity,musicDetailsData);
        listView.setAdapter(adapter);
    }
}
